package gui.testSetup;

import common.Constants;
import gui.testSetup.components.TestConfSetting;
import gui.testSetup.components.PageDescriptionViewer;
import gui.testSetup.components.PageInputEditor;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import testGenerator.Controller;

/**
 *
 * @author William Whitney
 */
public class SetupEditor extends JFrame
{
    private static final long serialVersionUID = 1L;

    private final TestConfSetting currPage;
    private static final Dimension k_windowSize = new Dimension(600, 500);
    private JPanel panel;
    private final PageDescriptionViewer pageDescriptionViewer;
    private final PageInputEditor pageInputEditor;
    private final Controller control;
    private static final String cancelConfirmationQuestion = "Are you sure you want to cancel your changes?";
    private static final String cancelConfirmationTitle = "Warning";
    
    private boolean anyChangesMade = false;

    /**
     * Default constructor.
     * @param control 
     * @param enumPageType
     */
    public SetupEditor(Controller control, TestConfSetting enumPageType)
    {
        this.control = control;
        this.currPage = enumPageType;

        pageDescriptionViewer = new PageDescriptionViewer(currPage.getDescription());
        pageInputEditor = new PageInputEditor(currPage);
        pageInputEditor.setValue(this.control.getSetupInfo(currPage));
        
        pageInputEditor.addDocumentListener(new DocumentListener() {

            @Override
            public void insertUpdate(DocumentEvent e) {
                markChangeMade();
            }

            @Override
            public void removeUpdate(DocumentEvent e) {
                markChangeMade();
            }

            @Override
            public void changedUpdate(DocumentEvent e) {
                markChangeMade();
            }
            
        });

        //Setup frame
        this.setupFrame();
        
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);

        //Get The title
        panel.add(this.getPageTitleBar(), BorderLayout.NORTH);

        //Add Input Editor
        panel.add(this.pageInputEditor, BorderLayout.CENTER);

        //Add button bar
        this.panel.add(getButtonBar(), BorderLayout.SOUTH);

        this.setVisible(true);

    }

    private void setupFrame()
    {
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

        //Set size
        this.setMinimumSize(k_windowSize);
        this.setMaximumSize(k_windowSize);
        this.setPreferredSize(k_windowSize);

        //Set title
        this.setTitle(currPage.getTitle() + " Editor");

        //Create panel
        this.panel = new JPanel();
        this.panel.setLayout(new BorderLayout());
        this.setContentPane(panel);

    }

    /**
     * Show UI.
     */
    public void showUI()
    {
    }

    private JPanel getButtonBar()
    {
        JPanel btnPanel = new JPanel();
        btnPanel.setLayout(new FlowLayout());

        //Cancel button
        JButton cancel = new JButton("Cancel");
        cancel.addActionListener(new ActionListener()
        {

            @Override
            public void actionPerformed(ActionEvent e)
            {
                int option = JOptionPane.YES_OPTION;
                
                if (anyChangesMade) {
                    option = JOptionPane.showOptionDialog(panel, cancelConfirmationQuestion, cancelConfirmationTitle,
                                JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, null, null);
                }
                
                if (option == JOptionPane.YES_OPTION) {
                    dispose();
                }
            }
        });
        btnPanel.add(cancel);

        //Save btn
        JButton save = new JButton("Save");
        save.addActionListener(new ActionListener()
        {

            @Override
            public void actionPerformed(ActionEvent e)
            {
                control.setTestConf(currPage, pageInputEditor.getValue());
                dispose();
            }
        });
        btnPanel.add(save);
        return btnPanel;
    }

    /**
     * Returns a JPanel that is the title bar.
     * @return JPanel that has page title and other related info
     */
    public final JPanel getPageTitleBar()
    {
        JPanel titlePanel = new JPanel();
        titlePanel.setLayout(new BorderLayout());

        //North
        JLabel pageTitle = new JLabel(currPage.getTitle());
        pageTitle.setFont(Constants.getHeadingFont());
        titlePanel.add(pageTitle, BorderLayout.NORTH);

        //Center
        titlePanel.add(pageDescriptionViewer, BorderLayout.CENTER);

        //South
        titlePanel.add(new JLabel("  "), BorderLayout.SOUTH);

        return titlePanel;
    }
    
    private void markChangeMade()
    {
        anyChangesMade = true;
    }
}
